import { watch } from "vue"
const CHANGEARR = ['checkbox','radio','select']
export const useRules = (state,field)=>{
    const addRules = ()=>{
        let mes = '不可为空'
        if(!!field.options.requiredHint){
            mes = field.options.requiredHint
        }
        state.rules.push({
            required: true,
            message: mes,
            trigger:CHANGEARR.includes(field.type) ?'change':'blur' 
        })
    }
    
    const clearRules = ()=>{
        state.rules = []
    }
    
    watch(
        ()=>field.options.required,
        ()=>{
            if(field.options.required){
                addRules()
            } else {
                clearRules()
            }
        },
        {
            immediate:true
        }
    )
    return {}
}